import { ref, reactive, type Reactive, type Ref } from 'vue'
import { defineStore, type StoreDefinition } from 'pinia'
import { httpDeletePpt, httpGetPptList } from '@/api/ai-ppt'
import { TypeQuery, PPTListItem, ListResponse } from '@/models/ai-ppt'

const useAiPptStore = defineStore('ai_ppt', () => {
  const searchQuery: Reactive<TypeQuery> = reactive({
    page: 1,
    limit: 15,
    type: 1,
  })
  const pptList: Ref<PPTListItem[]> = ref([])
  const pptTotal: Ref<number> = ref(0)

  const getPptList = async () => {
    const response = await httpGetPptList({...searchQuery})
    if (response === null) {
      return
    }
    const {list, total} = response
    pptList.value = list
    pptTotal.value = total
  }

  const deletePpt = async (id: number, type: number) => {
    const response = await httpDeletePpt(id, type).then(() => true).catch(() => false)
    if (response) {
      await getPptList()
    }
    return response
  }

  return {searchQuery, pptList, pptTotal, getPptList, deletePpt}
})

export default useAiPptStore